using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Security.Cryptography;

namespace CodePlex.Resourceful.Silverlight
{
    public static class SilverlightCompat
    {

        //public static byte[] ToHMACSHA1(Encoding encoding, string key, string input)
        //{
        //    string rt = SilverlightSHA1.hex_hmac_sha1(key, input);
        //    return Utils.FromBase16(rt);

        //}

        public static byte[] ToHMACSHA1(Encoding encoding, string key, string input)
        {
            HMACSHA1 rt = new HMACSHA1(encoding.GetBytes(key));
            byte[] inputBytes = encoding.GetBytes(input);
            rt.ComputeHash(inputBytes);
            return rt.Hash;
        }

        public static byte[] ToMD5(byte[] value)
        {
            string rt = SilverlightMD5.ToMD5Base16(value);
            return Utils.FromBase16(rt);
        }


        public static string ReadString(XmlReader reader)
        {
            if (reader.ReadState != ReadState.Interactive)
            {
                return string.Empty;
            }
            reader.MoveToElement();
            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.IsEmptyElement)
                {
                    return string.Empty;
                }
                if (!reader.Read())
                {
                    throw CodePlexException.Format("Invalid xml operation.");
                }
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    return string.Empty;
                }
            }

            StringBuilder rt = new StringBuilder();
            while (0L != (0x6018 & (((int)1) << ((int)reader.NodeType))))
            {
                rt.Append(reader.Value);
                if (!reader.Read())
                {
                    return rt.ToString();
                }
            }
            return rt.ToString();
        }

       
    }


}
